﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DelicateDict
{
    public partial class ClickAndSeeForm : Form
    {
        public static System.IntPtr curHandle = IntPtr.Zero;
        private string meanspace = "&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;";
        private string examspace = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;";
        private string phrasespace = "&nbsp;~&nbsp;";

        private Rectangle closeRec;
        private Rectangle autoCloseRec;
        private Rectangle titleRec;
        private static bool isAutoClose = true;
        private Point pos;
        private int isMax = -1;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetCursorPos(out Point lpPoint);

        public ClickAndSeeForm()
        {
            InitializeComponent();
        }

        private string GetMeanFromDatabase(string word)
        {
            Classes.IOFile iof = new Classes.IOFile();
            return Classes._Database.GetMean(word.Trim().ToLower(),iof.DefaultDictType);
        }
        private string DeleteReturnChar(string str)
        {
            string temp = str.Replace("\\n", "");
            return temp;
        }
        public void ShowClickAndSee(string word)
        {
            string mean = DeleteReturnChar(GetMeanFromDatabase(word));
            webBrowser1.Navigate("about:blank");
            if (webBrowser1.Document != null)
            {
                HtmlDocument d = webBrowser1.Document.OpenNew(true);
                d.Write("<HTML><BODY text = \"blue\">" + DeleteBreak(GetText(mean)) + "</BODY></HTML>");
            }
        }
        public string GetText(string str)
        {
            if (str.StartsWith("@") == true)
            {
                str = str.Substring(1);
            }
            return Manipulation(str);
        }
        public string Manipulation(string str)
        {
            int prevPos = 0;
            int pos = 0;
            StringBuilder strbder = new StringBuilder();

            if ((pos = str.IndexOf('@', prevPos)) > -1)
            {
                str = str + '@';
                while ((prevPos < str.Length) && ((pos = str.IndexOf('@', prevPos)) > -1))
                {
                    strbder.Append(GetTypeOfWord(str.Substring(prevPos, pos - prevPos)));
                    strbder.Append("<br>");
                    prevPos = pos + 1;
                }
            }
            else
                strbder.Append(GetTypeOfWord(str));

            return strbder.ToString();
        }
        public string GetTypeOfWord(string str)
        {
            int prevPos = 0;
            int pos = 0;
            StringBuilder strbder = new StringBuilder();
            if ((pos = str.IndexOf('*', prevPos)) > -1)
            {
                str = str + '*';
                str = str.Replace("!", "*" + phrasespace);
                string word = str.Substring(prevPos, pos - prevPos);
                strbder.Append("<font color = \"#dd0000\" size =\"4\"><b><i>" + word + "</i></b></font>");
                strbder.Append("<br>");
                prevPos = pos + 1;
                while ((prevPos < str.Length) && ((pos = str.IndexOf('*', prevPos)) > -1))
                {
                    strbder.Append(GetMeaning(str.Substring(prevPos, pos - prevPos), 0));
                    prevPos = pos + 1;
                }
            }
            else
                strbder.Append(GetMeaning(str, 1));
            return strbder.ToString();
        }
        public string GetMeaning(string str, int skip)
        {
            int prevPos = 0;
            int pos = 0;
            StringBuilder strbder = new StringBuilder();
            if ((pos = str.IndexOf('-', prevPos)) > -1)
            {
                string type = str.Substring(prevPos, pos - prevPos);
                if (skip == 0)
                {
                    strbder.Append("<font color = #00aaff>" + type + "</font>");
                    strbder.Append("<br>");
                }
                else
                {
                    strbder.Append("<font color = #ff0000>" + type + "</font>");
                    strbder.Append("<br>");
                }
                str = str + "#";
                prevPos = pos + 1;
                while ((prevPos < str.Length) && ((pos = str.IndexOf('#', prevPos)) > -1))
                {
                    strbder.Append(GetExamples(str.Substring(prevPos, pos - prevPos), 0));
                    strbder.Append("<br>");

                    prevPos = pos + 1;
                }
            }
            else
                strbder.Append(GetExamples(str, skip + 1));
            return strbder.ToString();
        }
        public string GetExamples(string str, int skip)
        {
            int prevPos = 0;
            int pos = 0;
            StringBuilder strbder = new StringBuilder();
            if ((pos = str.IndexOf('=', prevPos)) > -1)
            {
                string mean = str.Substring(prevPos, pos - prevPos);
                if (skip == 0)
                {
                    strbder.Append("<font color = #0000aa>" + meanspace + mean + "</font>");
                    strbder.Append("<br>");
                }
                else
                {
                    switch (skip)
                    {
                        case 1:
                            strbder.Append("<font color = #00aaff>" + mean + "</font>");
                            strbder.Append("<br>");
                            break;
                        case 2:
                            strbder.Append("<font color = #ff0000>" + mean + "</font>");
                            strbder.Append("<br>");
                            break;
                    }
                }
                str = str + '=';
                prevPos = pos + 1;
                while ((prevPos < str.Length) && ((pos = str.IndexOf('=', prevPos)) > -1))
                {
                    pos = str.IndexOf('=', prevPos);
                    strbder.Append("<font color = #f000ff>" + examspace + ReplacePlus(str.Substring(prevPos, pos - prevPos)) + "</font>");
                    strbder.Append("<br>");
                    prevPos = pos + 1;
                }
            }
            else
            {
                if (skip == 2)
                    strbder.Append("<font color = #ff0000>" + str + "</font>");
                else
                    strbder.Append("<font color = #0000aa>" + meanspace + str + "</font>");
            }

            return strbder.ToString();
        }
        public string ReplacePlus(string str)
        {
            StringBuilder strbder = new StringBuilder();
            int pos = str.IndexOf('+');
            if (pos == -1)
                return str;
            strbder.Append(str.Substring(0, pos));
            string strViMean = str.Substring(pos + 1);
            strbder.Append("<font color = \"#888888\">" + " : " + strViMean + "</font>");
            return strbder.ToString();
        }
        public string DeleteBreak(string str)
        {
            string originalStr = str;
            string strDel = "<br>";
            int pos1 = 0;
            int pos2 = 0;
            int start = 0;
            while ((pos1 = originalStr.IndexOf(strDel, start++)) > -1)
            {
                while ((pos1 + strDel.Length < originalStr.Length) && ((pos2 = originalStr.IndexOf(strDel, pos1 + strDel.Length)) > -1))
                {
                    if (pos2 == pos1 + strDel.Length)
                    {
                        originalStr = originalStr.Remove(pos2, strDel.Length);
                    }
                    else
                    {
                        start = pos2;
                        break;
                    }
                }
            }
            return originalStr;
        }

        private void On_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            titleRec = new Rectangle(0, 0, this.ClientSize.Width, 13);
            closeRec = new Rectangle(this.ClientSize.Width - 20, 0, 12, 12);
            autoCloseRec = new Rectangle(10, 0, 20, 12);

            g.DrawLine(Pens.DarkGray, new Point(0, 13), new Point(this.ClientSize.Width, 13));
            g.DrawLine(Pens.Chocolate, new Point(closeRec.Left + 3, closeRec.Top + 3), new Point(closeRec.Right - 2, closeRec.Bottom - 2));
            g.DrawLine(Pens.Chocolate, new Point(closeRec.Left + 3, closeRec.Bottom - 2), new Point(closeRec.Right - 2, closeRec.Top + 3));
            if (ClickAndSeeForm.isAutoClose == false)
                g.DrawString("*", new Font("Times New Roman", 10), Brushes.Chocolate, autoCloseRec);
            else
                g.DrawString("+", new Font("Times New Roman", 10), Brushes.Chocolate, autoCloseRec);
            if (this.Width > 200)
                g.DrawString("Dedicated  Dictionary", new Font("Times New Roman", 8), Brushes.Green, this.Width / 2 - 60, 0);
            g.Dispose();
        }

        private void On_Click(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Left) && (closeRec.Contains(e.Location)) == true)
            {
                this.Close();
            }
            if ((e.Button == MouseButtons.Left) && (autoCloseRec.Contains(e.Location)) == true)
            {
                isAutoClose = !isAutoClose;
                Invalidate(titleRec);
            }
        }
        private void On_Resize(object sender, EventArgs e)
        {
            Invalidate(titleRec);
        }

        private void On_DoubleClick(object sender, EventArgs e)
        {
            Point curPosScreen;
            GetCursorPos(out curPosScreen);
            Point curPosWindow = this.PointToClient(curPosScreen);
            if (autoCloseRec.Contains(curPosWindow) == false)
            {
                isMax = 0 - isMax;
                this.Height = this.Height + 200 * isMax;
                this.Width = this.Width + 200 * isMax;
            }

        }

        private void On_MouseDown(object sender, MouseEventArgs e)
        {
            pos = new Point(e.X, e.Y);
        }

        private void On_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Left = this.Left + (e.X - pos.X);
                this.Top = this.Top + (e.Y - pos.Y);
            }

        }
        private void On_Deactivate(object sender, EventArgs e)
        {
            if (ClickAndSeeForm.isAutoClose == true)
            {
                this.Opacity = this.Opacity - 0.1;
                SetForegroundWindow(ClickAndSeeForm.curHandle);
                timer1.Start();
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ClickAndSeeForm.curHandle == IntPtr.Zero)
            {
                timer1.Stop();
                this.Close();
            }
            else
            {
                SetForegroundWindow(ClickAndSeeForm.curHandle);
                this.Opacity = this.Opacity - 0.1;
                if (this.Opacity == 0)
                {
                    timer1.Stop();
                    this.Close();
                }
            }
        }
        private void On_Load(object sender, EventArgs e)
        {
            int heightMax = SystemInformation.VirtualScreen.Height;
            int widthMax = SystemInformation.VirtualScreen.Width;
            int clientTop = this.Location.Y + this.Size.Height;
            int clientRight = this.Location.X + this.Size.Height;
            if (clientTop > heightMax)
                this.Location = new Point(this.Location.X, this.Location.Y - this.Size.Height - 20);
            if (clientRight > widthMax)
                this.Location = new Point(this.Location.X - this.Size.Width, this.Location.Y);
        }


    }
}
